package com.charles.seckillPlatform.controller;

import com.charles.seckillComponentCommon.constants.CacheConstants;
import com.charles.seckillComponentCommon.domain.AjaxResult;
import com.charles.seckillComponentCommon.domain.TableInfo;
import com.charles.seckillComponentCommon.utils.StringUtils;
import com.charles.seckillComponentRedis.service.RedisService;
import com.charles.seckillComponentSecurity.utils.ThreadUtil;
import com.charles.seckillData.domain.SysAddr;
import com.charles.seckillData.domain.SysOrder;
import com.charles.seckillPlatform.form.SubmitOrderForm;
import com.charles.seckillPlatform.mapper.SysAddrMapper;
import com.charles.seckillPlatform.mapper.SysGoodMapper;
import com.charles.seckillPlatform.service.SysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class SysOrderController {

    @Autowired
    private SysOrderService sysOrderService;

    @Autowired
    private SysAddrMapper sysAddrMapper;

    @Autowired
    private RedisService redisService;
    
    @Autowired
    private SysGoodMapper sysGoodMapper;
    
    /**
     * 用户订单
     */
    @GetMapping("/getOrderList")
    public TableInfo getOrderList(Integer pageNum, Integer pageSize) {
        return sysOrderService.queryOrderList(Long.parseLong(ThreadUtil.getUserId()), pageNum, pageSize);
    }

    /**
     * 订单详情
     */
    @GetMapping("/getOrder/{orderId}")
    public AjaxResult getOrder(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(sysOrderService.getOrder(orderId));
    }

    /**
     * 提交订单(处理需要快递的订单)
     */
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody SubmitOrderForm submitOrderForm) {
        Long addr_id = submitOrderForm.getAddr_id();
        String orderId = submitOrderForm.getOrderId();
        
        SysAddr sysAddr = sysAddrMapper.queryAddrById(addr_id);
        SysOrder order = (SysOrder) redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);
        
        order.setReceiver_name(sysAddr.getReceiver());
        order.setReceiver_phone(sysAddr.getPhone());
        order.setReceiver_addr(sysAddr.getFull_addr());

        redisService.setCacheObject(CacheConstants.ORDER_ID + orderId, order);
        return AjaxResult.success();
    }

    /**
     * 检查订单是否需要地址，如果需要并且没有就返回 false
     */
    @GetMapping("/checkAddrNeeded/{orderId}")
    public AjaxResult checkAddrNeeded(@PathVariable("orderId") String orderId) {
        SysOrder order = (SysOrder) redisService.getCacheObject(CacheConstants.ORDER_ID + orderId);
        
        String isDelivery = sysGoodMapper.checkIsDelivery(order.getGood_id());
        
        if (isDelivery.equals("1") && (StringUtils.isEmpty(order.getReceiver_name()) || StringUtils.isEmpty(order.getReceiver_addr()) || StringUtils.isEmpty(order.getReceiver_phone()))) {
            return AjaxResult.success(false);
        }
        return AjaxResult.success(true);
    }

    /**
     * 订单第三方支付成功响应
     */
    @PostMapping("/orderPaySuccess/{orderId}")
    public void orderPaySuccess(@PathVariable("orderId") String orderId) {
        sysOrderService.payOrderSuccess(orderId, "2");
    }

    /**
     * 检查订单是否完成
     */
    @GetMapping("/checkOrderIsFinished/{orderId}")
    public AjaxResult checkOrderIsFinished(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(sysOrderService.checkOrderIsFinished(orderId));
    }


}
